local yuanxue = fk.CreateSkill {
  name = "fk__yuanxue"
}

Fk:loadTranslationTable{
  ["fk__yuanxue"] = "远斈",
  [":fk__yuanxue"] = "当你使用的实体牌指定唯一目标后，你可以移去一张与此牌类别相同的“经注”，视为对该角色使用一张任意基本牌或普通锦囊牌。",

  ["#fk__yuanxue-use"] = "远斈：可移去一张 %arg“经注”，视为对 %dest 使用一张任意基本牌或普通锦囊牌",
  ["#fk__yuanxue-remove"] = "远斈：移去一张“经注”",

  ["$fk__yuanxue1"] = "君子于学，当穷书籍、厚德行。",
  ["$fk__yuanxue2"] = "雕凿诗礼，钻之弥坚；追述先圣，行之弥远。",
}

local U = require "packages/utility/utility"

yuanxue:addEffect(fk.TargetSpecified, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(yuanxue.name)
      and not data.card:isVirtual() and data.card.name == Fk:getCardById(data.card.id, true).name
      and data:isOnlyTarget(data.to) and not data.to.dead
      and table.find(player:getPile("fk__jingzhu"), function (id)
        return Fk:getCardById(id).type == data.card.type
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = data.to
    local ids = table.filter(U.getUniversalCards(room, "bt"), function (id)
      local card = Fk:getCardById(id)
      -- 注意不能对自己打南蛮，对别人开无中
      local fix = card:getFixedTargets(player, {bypass_distances = true, bypass_times = true})
      if fix and not table.contains(fix, to) then return false end
      return player:canUseTo(card, to, {bypass_distances = true, bypass_times = true})
    end)
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = false,
      pattern = tostring(Exppattern{ id = ids }),
      prompt = "#fk__yuanxue-use::"..to.id..":"..data.card:getTypeString(),
      expand_pile = ids,
      skill_name = yuanxue.name,
    })
    if #cards > 0 then
      event:setCostData(self, Fk:getCardById(cards[1]).name)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local pile = table.filter(player:getPile("fk__jingzhu"), function (id)
      return Fk:getCardById(id).type == data.card.type
    end)
    if #pile == 0 then return end
    if #pile > 1 then
      pile = {room:askToChooseCard(player, {
        target = player,
        flag = {card_data = {{ "fk__jingzhu", pile }}},
        skill_name = yuanxue.name,
        prompt = "#fk__yuanxue-remove"
      })}
    end
    room:moveCards({
      ids = pile,
      from = player,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile,
      skillName = yuanxue.name,
      proposer = player,
    })
    room:useVirtualCard(event:getCostData(self), nil, player, data.to, yuanxue.name, true)
  end,
})

return yuanxue
